package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationQuery;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;

    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.query4List(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Destination> toastsList(Long parentId) {
        //创建目的地集合
        List<Destination> list = new ArrayList<>();
        //查询当前点击的目的地,存入列表
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination != null){
            list.add(destination);
            //判断是否有父目的地
            while (destination.getParent() != null) {
                //查询父目的地,并取代变量目的地的值
                destination = destinationMapper.selectByPrimaryKey(destination.getParent().getId());
                list.add(destination);
            }
        }
        for (Destination d : list) {
            System.out.println(d.getName());
        }
        //反转列表
        Collections.reverse(list);
        return list;
    }

    @Override
    public AjaxResult changeHotState(Long id, int hot) {
        AjaxResult result = new AjaxResult();
        try {
            destinationMapper.updateHotState(id,hot);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @Override
    public AjaxResult setInfo(Long id, String info) {
        AjaxResult result = new AjaxResult();
        try {
            destinationMapper.updateInfo(id,info);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @Override
    public List<Destination> selectAll() {
        return destinationMapper.selectAll();
    }

    @Override
    public List<Destination> listHotDestByParentId(Long parentId) {
        return destinationMapper.selectHotByParentId(parentId);
    }

    @Override
    public List<Destination> listHotDestByRegionId(Long regionId) {
        return destinationMapper.selectHotByRegionId(regionId);
    }

    @Override
    public Destination get(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public Destination getCountry(Long destId) {
        List<Destination> destinations = toastsList(destId);
        if (destinations != null) {
            return destinations.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long destId) {
        List<Destination> destinations = toastsList(destId);
        if (destinations != null && destinations.size() > 1) {
            return destinations.get(1);
        }
        return null;
    }

    @Override
    public List<Destination> queryDestination(QueryObject qo) {

        List<Destination> destinations = destinationMapper.selectDestinationByQO(qo);
        return destinations;
    }

    /**
     * 查询用户所有的目的地收藏
     *
     * @param userId
     * @return
     */
    public List<Destination> getByUserId(Long userId) {
        List<Destination> destinations = destinationMapper.selectAllByUserId(userId);
        List<Destination> list = new ArrayList<>();
        for (Destination destination : destinations) {
            List<Destination> toastsList = toastsList(destination.getId());
            destination.setParent(toastsList.get(0));
            list.add(destination);
        }
        return list;
    }

    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.selectDestByDeep(deep);
    }

}
